package com.jy.adscm.wechat.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.wechat.bean.*;
import com.jy.adscm.wechat.mapper.mysql.WechatRepairMapper;
import com.jy.adscm.wechat.mapper.mysql.WechatSelfRepairMapper;
import com.jy.adscm.wechat.mapper.mysql.WechatUserMapper;
import com.jy.adscm.wechat.mapper.mysql.WechatUserRoleMapper;
import com.jy.adscm.wechat.service.WechatRepairService;
import com.jy.adscm.wechat.service.WechatSelfRepairService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.wechat.service.WechatUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-09-26
 */
@Service
public class WechatSelfRepairServiceImpl extends ServiceImpl<WechatSelfRepairMapper, WechatSelfRepair> implements WechatSelfRepairService {
    @Autowired
    WechatUserMapper wechatUserMapper;
    @Autowired
    WechatRepairService wechatRepairService;
    @Autowired
    WechatUserRoleService wechatUserRoleService;
     @Transactional(rollbackFor = Exception.class)
    @Override
    public WechatSelfRepair insertWechatSelfRepair(WechatSelfRepair wechatSelfRepair)  throws AdscmSystemException {
          //查找最大的自修单号
         synchronized (this){
             String orderNums="";
             String date= new SimpleDateFormat("yyMMdd").format(new Date());
             Wrapper<WechatSelfRepair> wrapper =new EntityWrapper<>();
             wrapper.like("order_nums",date, SqlLike.RIGHT);
             wrapper.orderBy("order_nums",false);
             wrapper.last("limit 1");
             List<WechatSelfRepair> wechatSelfRepairs = super.selectList(wrapper);
             if (wechatSelfRepairs!=null && wechatSelfRepairs.size()>0){
                 orderNums= String.valueOf(Long.valueOf(wechatSelfRepairs.get(0).getOrderNums())+1) ;

             }else {
                 orderNums=date+"0001";
             }
             wechatSelfRepair.setOrderNums(orderNums);
             super.insert(wechatSelfRepair);
            return wechatSelfRepair;
         }
    }

    @Override
    public Page<WechatSelfRepair> selectWechatSelfRepair(Page<WechatSelfRepair> page, QueryWechatSelfRepair queryWechatSelfRepair)throws AdscmSystemException {
        WechatUser wechatUser = wechatUserMapper.selectByOpenId(queryWechatSelfRepair.getOpenid());
        if (!AdscmBaseUtils.getInstance().isNotEmpty(wechatUser,null)){
           return page;
        }
        //查询是否是浆站人员
        Boolean jzUser = wechatUserRoleService.isJzUser(queryWechatSelfRepair.getOpenid());
        Wrapper<WechatSelfRepair> wrapper=new EntityWrapper<>();
        if (jzUser){
            wrapper.eq("repairer",wechatUser.getId());
        }
        page=super.selectPage(page,wrapper);
        List<WechatSelfRepair> wechatSelfRepairs = page.getRecords();
        for (WechatSelfRepair wechatSelfRepair : wechatSelfRepairs) {
               if (AdscmBaseUtils.getInstance().isNotEmpty(wechatSelfRepair.getPid(),null)){
                   WechatRepair wechatRepair = wechatRepairService.selectWechatRepairById(wechatSelfRepair.getPid());
                   wechatSelfRepair.setWechatRepair(wechatRepair);
               }
               if (AdscmBaseUtils.getInstance().isNotEmpty(wechatSelfRepair.getRepairer(),null)){
                   WechatUser wechatUser1 = wechatUserMapper.selectById(wechatSelfRepair.getRepairer());
                   wechatSelfRepair.setRepairerName(wechatUser1.getName());
               }
        }
        page.setTotal(selectCount(wrapper));
        return page;
    }

    @Override
    public boolean updateWechatSelfRepair(WechatSelfRepair wechatSelfRepair) {

        WechatSelfRepair oldWechatSelfRepair = super.selectById(wechatSelfRepair.getId());
        //已经处理，有了处理结果了
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatSelfRepair.getResult(),null)){
            if (!AdscmBaseUtils.getInstance().isNotEmpty(oldWechatSelfRepair.getResult(),null)){
                wechatSelfRepair.setState(1);
            }
        }
        boolean b = super.updateById(wechatSelfRepair);
        return b;
    }

    @Override
    public WechatSelfRepair selectWechatSelfRepairById(Long id) {
        WechatSelfRepair wechatSelfRepair = super.selectById(id);
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatSelfRepair.getPid(),null)){
            WechatRepair wechatRepair = wechatRepairService.selectWechatRepairById(wechatSelfRepair.getPid());
            wechatSelfRepair.setWechatRepair(wechatRepair);
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatSelfRepair.getRepairer(),null)){
            WechatUser wechatUser1 = wechatUserMapper.selectById(wechatSelfRepair.getRepairer());
            wechatSelfRepair.setRepairerName(wechatUser1.getName());
        }
        return wechatSelfRepair;
    }

    @Override
    public WechatSelfRepair selectByPid(Long id) {
         Wrapper<WechatSelfRepair> wrapper=new EntityWrapper<>();
         wrapper.eq("pid",id);
        List<WechatSelfRepair> wechatSelfRepairs = super.selectList(wrapper);

        if (wechatSelfRepairs!=null && wechatSelfRepairs.size()>0){
            WechatSelfRepair wechatSelfRepair = wechatSelfRepairs.get(0);
            return wechatSelfRepair;
        }
        return null;
    }
}
